/*
 *  类描述待补充.
 *
 * VERSION  DATE        BY              REASON
 * -------- ----------- --------------- ------------------------------------------
 * 1.00     2015-10-29  wxg      程序.发布
 * -------- ----------- --------------- ------------------------------------------
 * Copyright 2015 easycode2  System. - All Rights Reserved.
 *
 */
package cn.com.softvan;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class YoudaoTranslate {

	private String url = "http://fanyi.youdao.com/openapi.do";

	private String keyfrom = "wuxiaogangtest1";
	private String key = "1069496433";

	private String doctype = "json";

	private String sendGet(String str) {
		// 编码成UTF-8
		try {
			str = URLEncoder.encode(str, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		StringBuffer buf = new StringBuffer();
		buf.append("keyfrom=");
		buf.append(keyfrom);
		buf.append("&key=");
		buf.append(key);
		buf.append("&type=data&doctype=");
		buf.append(doctype);
		buf.append("&version=1.1&q=");
		buf.append(str);
		String param = buf.toString();
		String result = "";
		BufferedReader in = null;
		try {
			String urlName = url + "?" + param;
			URL realUrl = new URL(urlName);
			// 打开和URL之间的连接
			URLConnection conn = realUrl.openConnection();
			// 设置通用的请求属性
			conn.setRequestProperty("accept", "*");

			in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

			String lines = "";
			while ((lines = in.readLine()) != null) {
				result += lines;
			}
//			System.out.println("====="+result);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	public String getYouDaoValue(String str) {
		String result = null;
		// 发送GET请求翻译
//		result = sendGet(str);
		result="{\"translation\":[\"TongLi club\"],\"query\":\"童励俱乐部\",\"errorCode\":0}";
//		// 处理JSON的值
		JSONObject jo=JSONObject.fromObject(result);
		String in=jo.getString("errorCode");
//		String in = result.substring(re1 + 11, re2);
//		System.out.println("===========翻译是否成功============" + in);
		if (in.equals("0")) {
			System.out.println("翻译正常");
			System.out.println("==========有道翻译================" + jo.getString("query"));
			JSONArray ja=new JSONArray().fromObject(jo.getString("translation"));
			System.out.println("==========有道词典-网络释义================" +ja.getString(0) );
		} else if (in.equals("20")) {
			System.out.println("要翻译的文本过长");
			return "要翻译的文本过长";
		} else if (in.equals("30")) {
			System.out.println("无法进行有效的翻译");
			return "无法进行有效的翻译";
		} else if (in.equals("40")) {
			System.out.println("不支持的语言类型");
			return "不支持的语言类型";
		} else if (in.equals("50")) {
			System.out.println("无效的key");
			return "无效的key";
		}
		return result;
	}

	public static void main(String[] args) {
		String str = "童励俱乐部";
		YoudaoTranslate test = new YoudaoTranslate();
		String temp = test.getYouDaoValue(str);
		System.out.println(temp);
	}
}
